{
LANG : PASCAL
PROB : comehome
}
program COMEHOME;
const
  fi = 'comehome.in';
  fo = 'comehome.out';
  max = 52;
var
  d : array[1..max, 1..max] of longint;
  n, m : longint;

procedure inp;
var
  i, u, v, w : longint;
  p, q : char;
begin
  readln(m);
  fillchar(d, sizeof(d), $30);
  for i := 1 to m do
  begin
    if not seekeoln then read(p);
    if not seekeoln then read(q);
    if ord(p) > 96 then u := ord(p) - 96 + 26
    else u := ord(p) - 64;
    if ord(q) > 96 then v := ord(q) - 96 + 26
    else v := ord(q) - 64;
    readln(w);
    if w < d[u, v] then
    begin
      d[u, v] := w;
      d[v, u] := w;
    end;
  end;
end;

procedure process;
var
  i, j, k : integer;
begin
  for k := 1 to 52 do
    for i := 1 to 52 do
      for j := i+1 to 52 do
        if d[i, j] > d[i, k] + d[k, j] then
        begin
          d[i, j] := d[i, k] + d[k, j];
          d[j, i] := d[i, j];
        end;
end;

procedure out;
var
  i, v : integer;
  min : longint;
begin
  min := maxlongint;
  for i := 1 to 25 do
    if min > d[i, 26] then
    begin
      min := d[i, 26];
      v := i;
    end;
  writeln(chr(v+64), ' ', min);
end;

begin
  assign(input, fi); reset(input);
  assign(output, fo); rewrite(output);
  inp;
  process;
  out;
  close(output);
  close(input);
end.